package Search;

public class BlockSearch {
    //分块查找
    public static void main(String[] args) {
        int arr[]={27,30,40,36,13,19,16,20,7,10,43,50,48};
        block b1=new block(22,40,0,4);
        block b2=new block(13,20,5,8);
        block b3=new block(7,10,9,10);
        block b4=new block(43,50,11,12);
        block[] blocks={b1,b2,b3,b4};
        int result = blockSearch(blocks, arr, 48);
        System.out.println(result);


    }
    //寻找对应块内的索引
    public static int blockSearch(block[] blocks,int[] arr,int key){
        int blockIndexSearch = blockIndexSearch(blocks, key);
        if(blockIndexSearch==-1){
            return -1;
        }
        int sindex=blocks[blockIndexSearch].getSinedex();
        int eindex=blocks[blockIndexSearch].getEindex();
        System.out.println(sindex);
        System.out.println(eindex);
        for (int i =sindex; i <= eindex; i++) {
            if(arr[i]==key){
                return i;
            }
        }
        return -1;
    }
    //寻找属于那个块内
    public static int blockIndexSearch(block[] blocks,int key){
        for (int i = 0; i < blocks.length; i++) {
            if(key>=blocks[i].getMin() && key<=blocks[i].getMax()){
                return i;
            }
        }
        return -1;
    }



}
    class block{
        private int min;
        private int max;
        private int sinedex;
        private int eindex;

        public block() {
        }

        public block(int min, int max, int sinedex, int eindex) {
            this.min = min;
            this.max = max;
            this.sinedex = sinedex;
            this.eindex = eindex;
        }

        /**
         * 获取
         * @return min
         */
        public int getMin() {
            return min;
        }

        /**
         * 设置
         * @param min
         */
        public void setMin(int min) {
            this.min = min;
        }

        /**
         * 获取
         * @return max
         */
        public int getMax() {
            return max;
        }

        /**
         * 设置
         * @param max
         */
        public void setMax(int max) {
            this.max = max;
        }

        /**
         * 获取
         * @return sinedex
         */
        public int getSinedex() {
            return sinedex;
        }

        /**
         * 设置
         * @param sinedex
         */
        public void setSinedex(int sinedex) {
            this.sinedex = sinedex;
        }

        /**
         * 获取
         * @return eindex
         */
        public int getEindex() {
            return eindex;
        }

        /**
         * 设置
         * @param eindex
         */
        public void setEindex(int eindex) {
            this.eindex = eindex;
        }

        public String toString() {
            return "block{min = " + min + ", max = " + max + ", sinedex = " + sinedex + ", eindex = " + eindex + "}";
        }
    }
